package com.yswy.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternUtil {


    public static void main(String[] args) {
        String video =    "https://v3-vvecloud.yangyi08.com/33bc2cc1aaa1f424d48c8a1436242c32/6336b3f1/video/tos/cn/tos-cn-v-07ec30/224dc306b7e44290a03af44d32779ea7/?a=7073\\u0026ch=0\\u0026cr=0\\u0026dr=4\\u0026er=0\\u0026lr=unwatermarked\\u0026cd=0%7C0%7C0%7C0\\u0026br=2720\\u0026bt=2720\\u0026cs=0\\u0026ds=3\\u0026ft=Q3AOVNnnrThWH6l1ba-bmo\\u0026mime_type=video_mp4\\u0026qs=13\\u0026rc=amp2Ojg6Zm9kZjQzNGg7M0Bpamp2Ojg6Zm9kZjQzNGg7M0AvZl5rcjRfX29gLS1kMDBzYSMvZl5rcjRfX29gLS1kMDBzcw%3D%3D\\u0026l=202209301613190102120910910545813E";
        String videoType = PatternUtil.getSubUtilSimple(video , "mime_type=");
        System.out.println(videoType);
    }
    /**
     * 正则表达式匹配两个指定字符串中间的内容
     *
     * @param soap
     * @return
     */
    public static List<String> getSubUtil(String soap, String rgex) {
        List<String> list = new ArrayList<String>();
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        return list;
    }

    /**
     * 返回单个字符串，若匹配到多个的话就返回第一个，方法与getSubUtil一样
     *
     * @param soap
     * @param rgex
     * @return
     */
    public static String getSubUtilSimple(String soap, String rgex) {
        List<String> list = new ArrayList<String>();
        List<String> extvounoLists = new ArrayList<String>();
        String userId = "";
        Pattern pattern = Pattern.compile(soap);
        Matcher m = pattern.matcher(rgex);
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        for (String str : list) {
            String[] strs = str.split("-");
            String strss = strs[strs.length - 1];
            extvounoLists.add(strs[strs.length - 1]);
        }

        for (String string : extvounoLists) {
            userId = string;
        }
        return userId;
    }

    public static Map getQueryParams(String url) {
        try {
            Map params = new HashMap();

            String[] urlParts = url.split("\\?");

            if (urlParts.length > 1) {
                String query = urlParts[1];

                for (String param : query.split("&")) {
                    String[] pair = param.split("=");

                    String key = URLDecoder.decode(pair[0], "UTF-8");

                    String value = "";

                    if (pair.length > 1) {
                        value = URLDecoder.decode(pair[1], "UTF-8");

                    }
                    params.put(key, value);
                }

            }
            return params;

        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError(ex);

        }

    }
}
